﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace WeatherSystem.API.Migrations
{
    /// <inheritdoc />
    public partial class StatisticsEavMigration : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_WeatherStatistics_Cities_CityId",
                table: "WeatherStatistics");

            migrationBuilder.DropPrimaryKey(
                name: "PK_WeatherStatistics",
                table: "WeatherStatistics");

            migrationBuilder.RenameTable(
                name: "WeatherStatistics",
                newName: "WeatherStatistics_Legacy");

            migrationBuilder.AddPrimaryKey(
                name: "PK_WeatherStatistics_Legacy",
                table: "WeatherStatistics_Legacy",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "StatisticsEntries",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    CityId = table.Column<Guid>(type: "TEXT", nullable: false),
                    StatisticType = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
                    StatisticDate = table.Column<DateTime>(type: "TEXT", nullable: false),
                    DataPointCount = table.Column<int>(type: "INTEGER", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_StatisticsEntries", x => x.Id);
                    table.ForeignKey(
                        name: "FK_StatisticsEntries_Cities_CityId",
                        column: x => x.CityId,
                        principalTable: "Cities",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "StatisticValues",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    StatisticsEntryId = table.Column<Guid>(type: "TEXT", nullable: false),
                    MetricKey = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
                    AggregationType = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
                    RawValue = table.Column<string>(type: "TEXT", maxLength: 500, nullable: false),
                    DataType = table.Column<string>(type: "TEXT", maxLength: 20, nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_StatisticValues", x => x.Id);
                    table.ForeignKey(
                        name: "FK_StatisticValues_MetricDefinitions_MetricKey",
                        column: x => x.MetricKey,
                        principalTable: "MetricDefinitions",
                        principalColumn: "Key",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_StatisticValues_StatisticsEntries_StatisticsEntryId",
                        column: x => x.StatisticsEntryId,
                        principalTable: "StatisticsEntries",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.UpdateData(
                table: "Users",
                keyColumn: "Id",
                keyValue: new Guid("77777777-7777-7777-7777-777777777777"),
                column: "UpdatedAt",
                value: new DateTime(2025, 9, 1, 12, 19, 30, 516, DateTimeKind.Utc).AddTicks(1628));

            migrationBuilder.CreateIndex(
                name: "IX_StatisticsEntry_CityId_StatisticType_StatisticDate",
                table: "StatisticsEntries",
                columns: new[] { "CityId", "StatisticType", "StatisticDate" });

            migrationBuilder.CreateIndex(
                name: "IX_StatisticsEntry_StatisticDate",
                table: "StatisticsEntries",
                column: "StatisticDate");

            migrationBuilder.CreateIndex(
                name: "IX_StatisticValue_EntryId_MetricKey_AggregationType",
                table: "StatisticValues",
                columns: new[] { "StatisticsEntryId", "MetricKey", "AggregationType" });

            migrationBuilder.CreateIndex(
                name: "IX_StatisticValue_MetricKey_AggregationType",
                table: "StatisticValues",
                columns: new[] { "MetricKey", "AggregationType" });

            migrationBuilder.AddForeignKey(
                name: "FK_WeatherStatistics_Legacy_Cities_CityId",
                table: "WeatherStatistics_Legacy",
                column: "CityId",
                principalTable: "Cities",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_WeatherStatistics_Legacy_Cities_CityId",
                table: "WeatherStatistics_Legacy");

            migrationBuilder.DropTable(
                name: "StatisticValues");

            migrationBuilder.DropTable(
                name: "StatisticsEntries");

            migrationBuilder.DropPrimaryKey(
                name: "PK_WeatherStatistics_Legacy",
                table: "WeatherStatistics_Legacy");

            migrationBuilder.RenameTable(
                name: "WeatherStatistics_Legacy",
                newName: "WeatherStatistics");

            migrationBuilder.AddPrimaryKey(
                name: "PK_WeatherStatistics",
                table: "WeatherStatistics",
                column: "Id");

            migrationBuilder.UpdateData(
                table: "Users",
                keyColumn: "Id",
                keyValue: new Guid("77777777-7777-7777-7777-777777777777"),
                column: "UpdatedAt",
                value: new DateTime(2025, 9, 1, 11, 59, 45, 381, DateTimeKind.Utc).AddTicks(9420));

            migrationBuilder.AddForeignKey(
                name: "FK_WeatherStatistics_Cities_CityId",
                table: "WeatherStatistics",
                column: "CityId",
                principalTable: "Cities",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }
    }
}
